<?php
    include("include/header.inc.php");
    include("include/func.inc.php");
    $_SESSION['page'] = 'event';
    
    //Function to display content if not theme found
    function display_theme_not_found($texts){
        echo '<h2>'.$texts['error'][$_SESSION['lang']].'</h2>';
        echo '<div class="tableauPage">';
        // Only the "event" parameter exists in the url
        //Error : theme not found
        echo '<p align="center">'.$texts['theme_not_found'][$_SESSION['lang']].'<br/><br/>';
        echo '<a href="./detailEvent.php?event='.$_GET['event'].'" data-role="button" data-inline="true">'.$texts['back'][$_SESSION['lang']].'</a>';
        echo '</p></div>';
    }
    
    echo'<div data-role="content">';
    
    //Check that the "theme" parameter exist in the url
    if(isset ($_GET['theme'])) {
        
        //SAP connection
        $rfc = connectSAP();
         //Array to stock all themes
        $themes_array = array();

        if(strpos($_GET['theme'],'-')==false) //Only one theme
            $themes_array[0] = $_GET['theme'];
        else //More than one theme
            $themes_array = explode("-", $_GET['theme']);

        if (count($themes_array)  > 1)
            echo '<h2>'. $texts['unregistered_to_themes'][$_SESSION['lang']] .'</h2>';
        else
            echo '<h2>'. $texts['unregistered_to_theme'][$_SESSION['lang']] .'</h2>';
        echo '<div class="tableauPage">';
        //Foreach themes
        for($i = 0; $i < sizeof($themes_array); ++$i){
            $id_theme = $themes_array[$i];
            //-- FUNCTION MODULE: Z_FM_M646_GET_THEMES --   
            //We must know if the function really exists
            $fce_get_themes = saprfc_function_discover($rfc, "Z_FM_M646_GET_THEMES");
            if(!$fce_get_themes){
                echo "The function module has failed.";
                echo $rfc;
                exit;
            }

            //Pass import parameters
            saprfc_import ($fce_get_themes,"IV_THEME",$id_theme);

            //Pass table parameters
            saprfc_table_init ($fce_get_themes,"TAB_DETAILS_THEME");

            //Call and execute the function
            $rc_get_themes = saprfc_call_and_receive ($fce_get_themes);            
            if ($rc_get_themes != SAPRFC_OK){
                if ($rfc == SAPRFC_EXCEPTION )
                    echo ("Exception raised: ".saprfc_exception($fce_get_themes));
                else
                    echo ("Call error: ".saprfc_error($fce_get_themes));
                exit;
            }

            //Fetch the data from the internal tables
            $data_row = saprfc_table_rows ($fce_get_themes,"TAB_DETAILS_THEME");
            if ($data_row > 0){//If the theme exist
                
                $DATARow = saprfc_table_read ($fce_get_themes,"TAB_DETAILS_THEME",1);

                //Current theme id
                $id_event = htmlentities($DATARow['EVENT_ID']);

                //Variables to display the date in a correct format
                $dateStart = substr($DATARow['STARTDATE'],-2).'.'.substr($DATARow['STARTDATE'],4,2).'.'.substr($DATARow['STARTDATE'],0,4);
                $dateEnd = substr($DATARow['ENDDATE'],-2).'.'.substr($DATARow['ENDDATE'],4,2).'.'.substr($DATARow['ENDDATE'],0,4); 

                //Title
                echo '<h3>'.htmlentities($DATARow['TITLE_THEME']).'</h3>';
                echo '<div style="margin-left:10px;">';
                //Display dates (one or more days)
                if($dateStart != $dateEnd )
                    echo '<p>'. $texts['from'][$_SESSION['lang']] .' '. $dateStart .' '. $texts['to'][$_SESSION['lang']] .' '. $dateEnd .'</p>';
                else
                    echo '<p>'. $dateStart .'</p>';
                echo '<p>'.htmlentities($DATARow['ADDRESS']).'<br/>';
                //Information
                echo $DATARow['ZIPCODE'].' '.htmlentities($DATARow['CITY']).'<br/>';
                echo $texts['room'][$_SESSION['lang']] .' : '.htmlentities($DATARow['ROOM']).'</p>';
                echo '</div>';                
            }
            else//These theme does not exist
                display_theme_not_found($texts);
        }
        
        echo '<p>'. $texts['for'][$_SESSION['lang']] .' : <b>'.$_SESSION['name'].'</b></p>';
            echo '<p><b>'.$texts['unregisterPreRequisPart1'][$_SESSION['lang']].htmlentities($DATARow['TITLE_THEME']).$texts['unregisterPreRequisPart2'][$_SESSION['lang']].'</b></p>';
        //Unsubscribe button
        echo'<p><a href="./unregisterValidateMultiple.php?theme='.$_GET['theme'].'" data-role="button" data-inline="true">'. $texts['validate'][$_SESSION['lang']] .'</a>';
        //Display cancel button
        echo '<a href="./historique.php" data-role="button" data-inline="true">'. $texts['cancel'][$_SESSION['lang']] .'</a></p>';
        echo '</div>';

        //realease the function and close the connection
        saprfc_function_free($fce_get_themes);

        saprfc_close($rfc);
    }
    else//The parameter "themes" does not exist
        display_theme_not_found($texts);
    
    echo '</div>';
    include('include/footer.inc.php');  // Call footer
?>